<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>歌手页面</title>
		<script src="../../js/vue.js"></script>
		<script src="../../js/element.js"></script>
		<script src="../../js/axios.min.js"></script>
		<link rel="stylesheet" type="text/css" href="../../css/element.css" />
		<link rel="stylesheet" type="text/css" href="../../fonts/iconfont/iconfont.css" />
		<link rel="stylesheet" type="text/css" href="../../css/singer/singer.css" />
	</head>
	<body>
		<div id="app">
			<!-- 头部歌手信息 -->
			<div class="header">
				<!-- 歌手图片 -->
				<div class="img">
					<img :src="singer.singerImg">
					<div class="singerInfo">
						<div class="singerName">{{singer.singerName}}</div>
						<div class="fan">粉丝数：3813.6万</div>
						<div> 
							<!-- 关注 -->
							<span class="care"><i class="iconfont icon-tianjia1"></i> 关注</span>
							<!-- 其他 -->
							<span class="otherButton">
								<i class="iconfont icon-gengduo"></i>
							</span>
						</div>
					</div>
				</div>

			</div>
			<div class="main">
				<div class="main-header">
					<span class="nav1" @click="navChange(1)">歌曲</span>
					<span class="nav2" @click="navChange(2)">专辑</span>
					<span class="nav3" @click="navChange(3)">介绍</span>
					<span id="hr"></span>
				</div>
				<div class="iframDiv">
					<iframe id="albumIframe" :src="navUrl" frameborder="0">
						
					</iframe>
				</div>
			</div>
		</div>
	</body>
	<script>
		new Vue({
			el: '#app',
			data: function() {
				return {
					url: '', //本页面的 url
					singer: '', //歌手信息
					// 菜单
					navUrl: 'song.html', //菜单指向的地址
					currentIndex: -1
				}
			},
			mounted() { // 生命周期函数
				this.getSingerById()
				window.getChildrenSongCurrentIndex = this.getChildrenSongCurrentIndex
				window.listPlayButton = this.listPlayButton
				window.getIndexCurrentIndex = this.getIndexCurrentIndex
				window.getChildrenAlbumId = this.getChildrenAlbumId
				window.toAlbum = this.toAlbum
				window.getSingerById = this.getSingerById
			},
			methods: {
				// 获取 album 点击的 id
				getChildrenAlbumId(id){
					const fatherHtml =  window.parent.document.querySelector("#indexIframe")
					fatherHtml.setAttribute('src', 'album/album.html?id='+id)
				},
				getChildrenSongCurrentIndex(currentIndex) {
					this.currentIndex = currentIndex
				},
				// 菜单选中为其添加样式
				navChange(navId) {
					console.log(navId)
					// 1、先去掉选择中的样式
					const navsDocument = document.querySelectorAll('.main-header span')
					for (var i = 0; i < navsDocument.length; i++) {
						navsDocument[i].style.color = '#000000'
					}
					this.navUrl = ''
					var singerId = this.getUrlSingerId()

					const hrDocument = document.querySelector('#hr')
					// 2、判断用户选中的是哪一个，加上样式
					if (navId == 1) {
						const nav1Document = document.querySelector('.nav1')
						nav1Document.style.color = '#FC6C62'
						hrDocument.style.left = '20px'
						this.navUrl = 'song.html?' + singerId
					} else if (navId == 2) {
						const nav1Document = document.querySelector('.nav2')
						nav1Document.style.color = '#FC6C62'
						hrDocument.style.left = '165px'
						this.navUrl = 'album.html?' + singerId
					} else if (navId == 3) {
						const nav1Document = document.querySelector('.nav3')
						nav1Document.style.color = '#FC6C62'
						hrDocument.style.left = '310px'
						this.navUrl = 'introduce.html?' + singerId
					}
				},
				// 获取当前页面的 url 中的 singerId
				getUrlSingerId() {
					// 1、获取当前页面的 url
					var url = window.location.href
					this.url = url
					console.log(url)
					// 2、字符串截取 获取 singerId
					var singerId = url.slice(url.indexOf('=') + 1)
					console.log('专辑id', singerId)
					// 3、返回 singerId
					return '' + singerId
				},
				// 根据 singerId 查找专辑信息
				getSingerById() {
					// 1、获取 singerId
					console.log(this.getUrlSingerId())
					var singerId = this.getUrlSingerId()
					axios({
						method: 'get',
						url: 'http://localhost:8160/singer/getOneById/' + singerId,
						headers: {
							'token': this.getCookie('yxy-pre:user_token')
						}
					}).then(res => {
						//处理返回的数据，如果登录成功，保留登录成功的状态，转向成功页面。如果登录不成功，提示用户
						if (res.data != undefined && res.data.code == 200) {
							console.log(res.data)
							this.singer = res.data.data.singerInfo
							// 将数据发送给子页面
							// 这里需要暴露给全局，这样的话，子页面才能调用相对应的方法
							window.toChildData = this.singer
						} else { //登陆失败
							this.$message.error(res.data.message)
						}
					})
				},
				// 播放全部
				listPlayButton(currentIndex) {
					console.log('album歌曲下标' + currentIndex)
					this.getSingerById()
					console.log(this.singer.songList)
					// 1、将歌手的歌曲列表数据通过父页面方法传给父页面
					// 获取正在播放歌曲的下标，-1 表示还没有播放
					window.parent.getChildrenSongList(this.singer.songList, currentIndex)
				},
				// 获取父页面正在播放的下标
				getIndexCurrentIndex(currentIndex) {
					// 调用子页面方法将，正在播放歌曲下标传给子页面
					var iframeDocument = document.getElementById("albumIframe")
					iframeDocument.contentWindow.getAlbumCurrentIndex(currentIndex)
				},
				// 通过值获取cookie
				getCookie(key) {
					// 如果浏览器中有多个cookie，会以 ；分隔 例如：a=a1;b=b1
					// 根据 ；将 cookie 字符串分割成数组 
					var cookieArr = document.cookie.split("; ");
					// 遍历数组
					for (var i = 0; i < cookieArr.length; i++) {
						// 再根据 = 分隔数组中的每一个元素，数组第一个就是key，第二个就是value
						var cookiePair = cookieArr[i].split("=");
						if (key == cookiePair[0]) {
							return decodeURIComponent(cookiePair[1]);
						}
					}
					return null;
				},
				toAlbum(id){
					const fatherHtml =  window.parent.document.querySelector("#indexIframe")
					fatherHtml.setAttribute('src', 'album/album.html?id='+id)
				},
			},
		})
	</script>
</html>